package cn.xyinping.shoppingcart.service.impl;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.service.impl.BasicServiceImpl;
import cn.xyinping.basic.util.LoginContextUtil;
import cn.xyinping.pet.domain.Pet;
import cn.xyinping.pet.mapper.PetMapper;
import cn.xyinping.shoppingcart.domain.ShoppingCart;
import cn.xyinping.shoppingcart.mapper.ShoppingCartMapper;
import cn.xyinping.shoppingcart.service.IShoppingCartService;
import cn.xyinping.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;


/**
 * @author 27266
 * @desrciption
 * @date 2021/11/10
 */
@Service
public class ShoppingCartServiceImpl extends BasicServiceImpl<ShoppingCart> implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    @Transactional
    public ShoppingCart addCart(Long id) {
        Logininfo logininfo = LoginContextUtil.getLoginContext();
        Pet pet = new Pet();
        if(pet.getStore_number()==null){
            throw new MyException("库存不足");
        }
        Pet pet1 = petMapper.loadById(id);
        ShoppingCart shoppingCart = new ShoppingCart();
        Date date = new Date();
        shoppingCart.setCreatTime(date);
        shoppingCart.setUser_id(logininfo.getId());
        shoppingCart.setState(ShoppingCart.SHOPPING_SHOW);
        shoppingCart.setName(pet1.getName());
        shoppingCart.setId(pet1.getId());
        shoppingCart.setResources(pet1.getResources());
        shoppingCart.setType_id(pet.getType_id());
        shoppingCart.setSaleprice(pet.getSaleprice());
        return shoppingCart;
    }
    // 查询购物车
    @Override
    public List<ShoppingCart> loadCarte() {
        Logininfo logininfo = LoginContextUtil.getLoginContext();
        Long id = logininfo.getId();
        return shoppingCartMapper.loadCarteByUserId(id);
    }


}
